package com.wsoft.form.manager;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsoft.constant.RedisKeyConstant;
import com.wsoft.core.annotation.GetRedisCache;
import com.wsoft.core.service.RedisService;
import com.wsoft.form.entity.WfFormTableRelationEntity;
import com.wsoft.form.mapper.WfFormTableRelationMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Author z001
 * @Date 2024/9/5 08:08
 */
@Service
public class WfFormTableRelationManager extends ServiceImpl<WfFormTableRelationMapper, WfFormTableRelationEntity> {
    @Resource
    private RedisService redisService;

    /**
     * 查询 form下 是否已经存在主表
     *
     * @param formId   formId
     * @param isMaster 0-非主表,1-主表
     * @return
     */
    public List<WfFormTableRelationEntity> getMasterTable(Long formId, Integer isMaster) {
        List<WfFormTableRelationEntity> list = getByFormId(formId);
        if (CollUtil.isEmpty(list)) {
            return new ArrayList<>();
        }
        return list.stream().filter(item -> Objects.equals(item.getIsMaster(), isMaster)).collect(Collectors.toList());
    }

    /**
     * 判断当前表 是否是主表/从表
     *
     * @param formId
     * @param tableId
     * @param isMaster
     * @return
     */
    public WfFormTableRelationEntity getMasterTable(Long formId, Long tableId, Integer isMaster) {
        QueryWrapper<WfFormTableRelationEntity> query = new QueryWrapper<>();
        query.lambda().eq(WfFormTableRelationEntity::getFormId, formId)
                .eq(WfFormTableRelationEntity::getTableId, tableId)
                .eq(null != isMaster, WfFormTableRelationEntity::getIsMaster, isMaster);
        return getOne(query);
    }

    /**
     * 获取主表关联关系
     *
     * @param formId        表单id
     * @param masterTableId 主表id
     * @return
     */
    public List<WfFormTableRelationEntity> getMasterRelation(Long formId, Long masterTableId) {
        List<WfFormTableRelationEntity> list = getByFormId(formId);
        if (CollUtil.isEmpty(list)) {
            return new ArrayList<>();
        }
        return list.stream().filter(item -> Objects.equals(item.getMasterTableId(), masterTableId)).collect(Collectors.toList());
    }

    /**
     * 根据slaveColumnId 查询
     * @param formId formId
     * @param slaveColumnId slaveColumnId
     * @return
     */
    public List<WfFormTableRelationEntity> getBySlaveColumnId(Long formId, Long slaveColumnId) {
        QueryWrapper<WfFormTableRelationEntity> query = new QueryWrapper<>();
        query.lambda().eq(WfFormTableRelationEntity::getFormId, formId)
                .eq(WfFormTableRelationEntity::getSlaveColumnId, slaveColumnId);
        return list(query);
    }



    /**
     * 根据 formId 查询
     *
     * @param formId
     */
    @GetRedisCache(type = RedisKeyConstant.Form.TABLE_RELATION_LIST,
            expire_sec = RedisKeyConstant.Form.EXPIRED,
            value = RedisKeyConstant.REDIS_SELF)
    public List<WfFormTableRelationEntity> getByFormId(Long formId) {
        QueryWrapper<WfFormTableRelationEntity> query = new QueryWrapper<>();
        query.lambda().eq(WfFormTableRelationEntity::getFormId, formId);
        return list(query);
    }

    /**
     * 根据 formId 删除
     *
     * @param formId
     */
    public boolean removeByFormId(Long formId) {
        QueryWrapper<WfFormTableRelationEntity> query = new QueryWrapper<>();
        query.lambda().eq(WfFormTableRelationEntity::getFormId, formId);
        boolean b = remove(query);
        if(b){
            redisService.deleteByKeyPattern(RedisKeyConstant.Form.TABLE_RELATION_PREFIX + RedisKeyConstant.REDIS_PATTERN);
        }
        return b;
    }

    /**
     * 删除主表
     *
     * @param tableId
     * @return
     */
    public boolean delByTableId(Long formId,Long tableId) {
        QueryWrapper<WfFormTableRelationEntity> query = new QueryWrapper<>();
        query.lambda().eq(WfFormTableRelationEntity::getTableId, tableId)
                .eq(WfFormTableRelationEntity::getFormId,formId);
        boolean b = remove(query);
        if(b){
            redisService.deleteByKeyPattern(RedisKeyConstant.Form.TABLE_RELATION_PREFIX + RedisKeyConstant.REDIS_PATTERN );
        }
        return b;
    }

    /**
     * 删除从表
     *
     * @param salveTableId
     * @return
     */
    public boolean delBySalveTableId(Long formId,Long salveTableId) {
        QueryWrapper<WfFormTableRelationEntity> query = new QueryWrapper<>();
        query.lambda().eq(WfFormTableRelationEntity::getSlaveTableId, salveTableId)
                .eq(WfFormTableRelationEntity::getFormId,formId);
        boolean b = remove(query);
        if(b){
            redisService.deleteByKeyPattern(RedisKeyConstant.Form.TABLE_RELATION_PREFIX + RedisKeyConstant.PATTERN_SYMBOL);
        }
        return b;
    }

    @GetRedisCache(type = RedisKeyConstant.Form.TABLE_RELATION_INFO,
            expire_sec = RedisKeyConstant.Form.EXPIRED,
            value = RedisKeyConstant.REDIS_SELF)
    public WfFormTableRelationEntity getById(Long id) {
        return super.getById(id);
    }

    /**
     * 保存
     *
     * @param entity
     * @return
     */
    @Override
    public boolean save(WfFormTableRelationEntity entity) {
        boolean b = super.save(entity);
        if(b){
            redisService.deleteByKeyPattern(RedisKeyConstant.Form.TABLE_RELATION_LIST + RedisKeyConstant.PATTERN_SYMBOL);
        }
        return b;
    }

    /**
     * 跟新
     *
     * @param entity
     * @return
     */
    @Override
    public boolean updateById(WfFormTableRelationEntity entity) {
        boolean b = super.updateById(entity);
        if (b) {
            redisService.del(RedisKeyConstant.Form.TABLE_RELATION_INFO  + entity.getId());
            redisService.deleteByKeyPattern(RedisKeyConstant.Form.TABLE_RELATION_LIST + RedisKeyConstant.PATTERN_SYMBOL);
        }
        return b;
    }

    /**
     * 移除
     *
     * @param id
     * @return
     */
    public boolean removeById(Long id) {
        boolean b = super.removeById(id);
        if (b) {
            redisService.del(RedisKeyConstant.Form.TABLE_RELATION_INFO  + id);
            redisService.deleteByKeyPattern(RedisKeyConstant.Form.TABLE_RELATION_LIST + RedisKeyConstant.PATTERN_SYMBOL);
        }
        return b;

    }
}
